added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CppShellExtContextMenuHandler / dllmain.cpp
blob2404948939d12f494ac724c309a7572ed4434269
1 /****************************** Module Header ******************************\
2 Module Name: dllmain.cpp
3 Project: CppShellExtContextMenuHandler
4 Copyright (c) Microsoft Corporation.
6 The file implements DllMain, and the DllGetClassObject, DllCanUnloadNow,
7 DllRegisterServer, DllUnregisterServer functions that are necessary for a COM
8 DLL.
10 DllGetClassObject invokes the class factory defined in ClassFactory.h/cpp and
11 queries to the specific interface.
13 DllCanUnloadNow checks if we can unload the component from the memory.
15 DllRegisterServer registers the COM server and the context menu handler in
16 the registry by invoking the helper functions defined in Reg.h/cpp. The
17 context menu handler is associated with the .cpp file class.
19 DllUnregisterServer unregisters the COM server and the context menu handler.
21 This source is subject to the Microsoft Public License.
22 See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
23 All other rights reserved.
25 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
26 EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
28 \***************************************************************************/
30 #include <windows.h>
31 #include <Guiddef.h>
32 #include "ClassFactory.h" // For the class factory
33 #include "Reg.h"
36 // {BFD98515-CD74-48A4-98E2-13D209E3EE4F}
37 // When you write your own handler, you must create a new CLSID by using the
38 // "Create GUID" tool in the Tools menu, and specify the CLSID value here.
39 const CLSID CLSID_FileContextMenuExt =
40 { 0xBFD98515, 0xCD74, 0x48A4, { 0x98, 0xE2, 0x13, 0xD2, 0x09, 0xE3, 0xEE, 0x4F } };
43 HINSTANCE g_hInst = NULL;
44 long g_cDllRef = 0;
47 BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
49 switch (dwReason)
51 case DLL_PROCESS_ATTACH:
52 // Hold the instance of this DLL module, we will use it to get the
53 // path of the DLL to register the component.
54 g_hInst = hModule;
55 DisableThreadLibraryCalls(hModule);
56 break;
57 case DLL_THREAD_ATTACH:
58 case DLL_THREAD_DETACH:
59 case DLL_PROCESS_DETACH:
60 break;
62 return TRUE;
67 // FUNCTION: DllGetClassObject
69 // PURPOSE: Create the class factory and query to the specific interface.
71 // PARAMETERS:
72 // * rclsid - The CLSID that will associate the correct data and code.
73 // * riid - A reference to the identifier of the interface that the caller
74 // is to use to communicate with the class object.
75 // * ppv - The address of a pointer variable that receives the interface
76 // pointer requested in riid. Upon successful return, *ppv contains the
77 // requested interface pointer. If an error occurs, the interface pointer
78 // is NULL.
80 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
82 HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
84 if (IsEqualCLSID(CLSID_FileContextMenuExt, rclsid))
86 hr = E_OUTOFMEMORY;
88 ClassFactory *pClassFactory = new ClassFactory();
89 if (pClassFactory)
91 hr = pClassFactory->QueryInterface(riid, ppv);
92 pClassFactory->Release();
96 return hr;
101 // FUNCTION: DllCanUnloadNow
103 // PURPOSE: Check if we can unload the component from the memory.
105 // NOTE: The component can be unloaded from the memory when its reference
106 // count is zero (i.e. nobody is still using the component).
108 STDAPI DllCanUnloadNow(void)
110 return g_cDllRef > 0 ? S_FALSE : S_OK;
115 // FUNCTION: DllRegisterServer
117 // PURPOSE: Register the COM server and the context menu handler.
119 STDAPI DllRegisterServer(void)
121 HRESULT hr;
123 wchar_t szModule[MAX_PATH];
124 if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0)
126 hr = HRESULT_FROM_WIN32(GetLastError());
127 return hr;
130 // Register the component.
131 hr = RegisterInprocServer(szModule, CLSID_FileContextMenuExt,
132 L"CppShellExtContextMenuHandler.FileContextMenuExt Class",
133 L"Apartment");
134 if (SUCCEEDED(hr))
136 // Register the context menu handler. The context menu handler is
137 // associated with the .cpp file class.
138 hr = RegisterShellExtContextMenuHandler(L".cpp",
139 CLSID_FileContextMenuExt,
140 L"CppShellExtContextMenuHandler.FileContextMenuExt");
143 return hr;
148 // FUNCTION: DllUnregisterServer
150 // PURPOSE: Unregister the COM server and the context menu handler.
152 STDAPI DllUnregisterServer(void)
154 HRESULT hr = S_OK;
156 wchar_t szModule[MAX_PATH];
157 if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0)
159 hr = HRESULT_FROM_WIN32(GetLastError());
160 return hr;
163 // Unregister the component.
164 hr = UnregisterInprocServer(CLSID_FileContextMenuExt);
165 if (SUCCEEDED(hr))
167 // Unregister the context menu handler.
168 hr = UnregisterShellExtContextMenuHandler(L".cpp",
169 CLSID_FileContextMenuExt);
172 return hr;